home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / cljune86.zip / XREF1.LTG < prev    next >
Text File  |  1986-04-07  |  8KB  |  336 lines

  1. #include <stdio.h>
  2.  
  3. #define MAX_DEF     200     /* maximum language definitions     */
  4. #define NAME_CHARS  "._"        /* Punctuation Allowed in Names         */
  5.  
  6. #define DQUOTE      0x22        /* ASCII Double Quote Character     */
  7. #define SQUOTE      0x27        /* ASCII Single Quote Character     */
  8.  
  9. FILE *fopen(), *infile, *outfile;
  10.  
  11. char    lbuff[120];        /* current line holder            */
  12. int    lcnt    = 1;        /* line counter starts at line 1    */
  13. int    lptr    = 0;        /* assume empty line buffer        */
  14. int    is_c    = FALSE;    /* is input source in "C"? Allow { }    */
  15.  
  16. struct numrec {
  17.   int number;            /* line number of symbol occurance    */
  18.   struct numrec *nnext;     /* next number of symbol line        */
  19.   };
  20.  
  21. struct symrec {
  22.   char *symbol;         /* symbol string            */
  23.   int  count;            /* symbol occurance counters        */
  24.   struct numrec *lnum;        /* line number records            */
  25.   struct symrec *lnode;     /* left branch of binary tree        */
  26.   struct symrec *rnode;     /* right branch of binary tree        */
  27.   };
  28.  
  29. struct symrec *symtab = NULL;    /* declare main symbol table        */
  30.  
  31. char *Exceptions[ MAX_DEF ];    /* language definition table structure    */
  32.  
  33. main( argc, argv )
  34. char *argv[];
  35. int argc;
  36. {
  37.   int c;
  38.  
  39.   if( (argc<2) || (argc>3) ){
  40.     printf("XRF: Usage is XRF [input name] <output name>\n");
  41.     exit();}
  42.  
  43.   if( NULL==(infile = fopen(argv[1],"r")) ){      /* open input file      */
  44.      printf("ERROR: Cannot open input file - %s\n",argv[1]);
  45.      exit();}
  46.  
  47.   if( argc==3 ){
  48.     if( NULL==(outfile = fopen( argv[2],"w")) ){   /* open output file     */
  49.        printf("ERROR: Cannot open output file - %s\n",argv[2]);
  50.        exit();}
  51.     printf("Generating Listing File - %s\n", argv[2] );
  52.     }
  53.   else{
  54.     outfile = stdout;
  55.     }
  56.  
  57.   load_tbl( argv[1] );              /* load language table      */
  58.   printf("Language Table Loaded\n");
  59.  
  60. /*    Main input and parse loop.                    */
  61.  
  62.   while( (c=getc(infile)) != EOF ) filter( c );
  63.  
  64.   fprintf( outfile, "Symbol Table for File - %s\n", upper(argv[1]) );
  65.  
  66.   symprint( symtab );
  67.  
  68.   printf("\nEnd.\n");
  69.  
  70. }                    /* end of main routine        */
  71.  
  72. /*                                    */
  73. /*    comment processor, quoted text, and punctuation         */
  74. /*                                    */
  75.  
  76. filter( c )
  77. char c;
  78. {
  79.   char c1;
  80.   static  is_com = FALSE;        /* assume not in comment    */
  81.  
  82.   if( c==DQUOTE ){            /* filter between double quotes  */
  83.     while( (c=getc(infile))!=DQUOTE ) if( c==EOF ) return;
  84.     next_char( ' ' );  }
  85.  
  86.   else if( c==SQUOTE ){         /* filter between single quotes  */
  87.     while( (c=getc(infile))!=SQUOTE ) if( c==EOF ) return;
  88.     next_char( ' ' );  }
  89.  
  90.   else if( (c=='{') && !is_c ){         /* if not "C" code, strip between {} */
  91.     while( (c=getc(infile))!='}' ) ;
  92.     next_char( ' ' );
  93.     }
  94.  
  95.   else if( c=='(' ) {                   /* remove all between ( * * ) pairs*/
  96.     c1 = getc(infile);
  97.     if( c1!='*' ){
  98.       next_char( ' ' );
  99.       ungetc( c1, infile );}
  100.     else {
  101.       is_com = TRUE;
  102.       while( is_com ){
  103.     while( (c=getc(infile))!='*' ) ;
  104.     c1 = getc(infile);
  105.     if( c1==')' ) is_com = FALSE;
  106.     else ungetc( c1, infile );
  107.       }                   /* end of while is_com  */
  108.       next_char( ' ' );
  109.     }                      /* end of else comment  */
  110.   }                      /* end of if paren      */
  111.  
  112.   else if( c=='/' ) {                   /* remove all between / * * / pairs */
  113.     c1 = getc(infile);
  114.     if( c1!='*' ){
  115.       next_char( ' ' );
  116.       ungetc( c1, infile );}
  117.     else {
  118.       is_com = TRUE;
  119.       while( is_com ){
  120.     while( (c=getc(infile))!='*' ) ;
  121.     c1 = getc(infile);
  122.     if( c1=='/' ) is_com = FALSE;
  123.     else ungetc( c1, infile );
  124.       }                   /* end of while is_com  */
  125.       next_char( ' ' );
  126.     }                      /* end of else comment  */
  127.   }                      /* end of if slash      */
  128.  
  129.   else{                 /* filter non-NAME_CHARS punctuation */
  130.     if( isalnum(c) || strchr(NAME_CHARS,c) || isspace(c) )
  131.       next_char( c );
  132.     else                /* special case to allow -> as token */
  133.       if( c=='-' ) {
  134.     if( '>' == (c=getc( infile )) ){  /* if - followed by > then allow   */
  135.       next_char( '-' );
  136.       next_char( '>' );
  137.       }
  138.     else {                /* else, put c back and send space   */
  139.       ungetc( c, infile );
  140.       next_char( ' ' );
  141.       }
  142.     }
  143.       else next_char(' ');              /* if not minus, send space          */
  144.     }
  145.  
  146. }
  147.  
  148. /*    symbol process (after comments and quotes have been stripped)    */
  149.  
  150. next_char( a )
  151. char a;
  152. {
  153.  
  154.   if( isspace(a) ){            /* filter excess white space    */
  155.     if( lptr==0 ){
  156.       if( a == '\n' ) ++lcnt;           /* count line in file           */
  157.       return;}
  158.     lbuff[lptr] = '\0';
  159.     lptr = 0;
  160.     next_sym( lbuff, lcnt );
  161.     if( a == '\n' ) ++lcnt;             /* count line in file           */
  162.     }
  163.   else{
  164.    lbuff[lptr++] = a;            /* insert character into buffer */
  165.    }
  166.  
  167. }
  168.  
  169. next_sym( s, n )
  170. char *s;
  171. int n;
  172. {
  173.   struct symrec *newsym();
  174.  
  175.   if( isdigit(s[0]) ) return;        /* filter strings beginning with # */
  176.  
  177.   if( is_lang(s)    ) return;        /* filter strings in Language Table */
  178.  
  179.   symtab = newsym( symtab, s, n );    /* insert new symbol in symbol table*/
  180.  
  181. }
  182.  
  183.  
  184. struct symrec *newsym( q, w, n )
  185. struct symrec *q;
  186. char *w;
  187. int n;
  188. {
  189.   struct symrec *salloc();
  190.   struct numrec *newnum();
  191.   char *strsave();
  192.   int cond;
  193.  
  194.   if( q==NULL ) {
  195.     q = salloc();        /* make a new node        */
  196.     upper( w );         /* save upper case        */
  197.     q->symbol = strsave( w );    /* store string in record    */
  198.     q->count = 1;
  199.     q->lnum = q->lnode = q->rnode = NULL;
  200.     q->lnum = newnum( q->lnum, lcnt ); }
  201.   else
  202.     if( (cond = strcmp(w, q->symbol)) == 0 ){    /* reoccurance    */
  203.       q->count++;
  204.       q->lnum = newnum( q->lnum, n );
  205.       }
  206.     else
  207.       if( cond<0 )        /* lower goes into left tree    */
  208.     q->lnode = newsym( q->lnode, w, n );
  209.       else
  210.     q->rnode = newsym( q->rnode, w, n );
  211.  
  212.   return( q );
  213.  
  214. }
  215.  
  216. struct numrec *newnum( p, n )
  217. struct numrec *p;
  218. int n;
  219. {
  220.   struct numrec *nalloc();
  221.  
  222.   if( p==NULL ){        /* new node at end of tree    */
  223.     p = nalloc();        /* create new storage element    */
  224.     p->number = n;        /* save number into space    */
  225.     p->nnext  = NULL; }
  226.   else{
  227.     p->nnext = newnum( p->nnext, n );}    /* continue along tree    */
  228.  
  229.   return( p );
  230.  
  231. }
  232.  
  233.  
  234. struct symrec *salloc()
  235. {
  236.   char *alloc();
  237.  
  238.   return( (struct symrec *) alloc( sizeof(struct symrec) ) );
  239.  
  240. }
  241.  
  242. struct numrec *nalloc()
  243. {
  244.   char *alloc();
  245.  
  246.   return( (struct numrec *) alloc( sizeof(struct numrec) ) );
  247.  
  248. }
  249.  
  250. char *strsave( str )
  251. char *str;
  252. {
  253.    char *p, *alloc();
  254.  
  255.    if( (p=alloc(strlen(str)+1)) != NULL ) strcpy( p, str );
  256.    if( p==NULL ) printf("ERROR: Out of string space.\n");
  257.  
  258.    return( p );
  259.  
  260. }
  261.  
  262. symprint( p )            /* print out symbol table        */
  263. struct symrec *p;
  264. {
  265.     if( p!=NULL ){
  266.       symprint(p->lnode);
  267.       fprintf(outfile, "%-15s (%d)\t:\n", p->symbol, p->count );
  268.       numprint(p->lnum, 0);
  269.       symprint(p->rnode);
  270.       }
  271. }
  272.  
  273. numprint( p, i )        /* print out page number table        */
  274. struct numrec *p;
  275. {
  276.    if( p!=NULL ){
  277.      fprintf( outfile, "  %4d", p->number );
  278.      if( !((i+1)%10) ) putc( '\n', outfile );
  279.      numprint( p->nnext, ++i );
  280.    }
  281.    else{
  282.      putc( '\n', outfile );
  283.      }
  284. }
  285.  
  286. is_lang( str )
  287. char *str;
  288. {
  289.   int i, j;
  290.  
  291.   i = 0;
  292.   while( Exceptions[i]!=NULL ){
  293.     if( ( j=strcmp( upper(str), Exceptions[i++] ) ) == 0 ) return( 1 );
  294.       else if( j<0 ) return( 0 );
  295.     }
  296.  
  297.   return( 0 );
  298.  
  299. }
  300.  
  301. load_tbl( iname )
  302. char *iname;
  303. {
  304.   char *cptr;
  305.   char fname[65], symbol[40];
  306.   FILE *tfile;
  307.   int i = 0;
  308.  
  309.   if( NULL == (cptr=strchr( iname, '.' ) )){
  310.     printf("ERROR: No extension on input file name: %s\n", iname );
  311.     exit();}
  312.  
  313.   sscanf( ++cptr, "%s", fname );
  314.   strcat( fname, ".XRF" );
  315.   upper( fname );
  316.  
  317.   if( 0 == strncmp( fname, "C.XRF", 5 ) ) is_c = TRUE;
  318.  
  319.   if( NULL==(tfile = fopen(fname,"r")) ){        /* open table file      */
  320.      printf("ERROR: Cannot open Language Table file: %s\n", fname );
  321.      exit();}
  322.  
  323.   while( i < MAX_DEF ){
  324.     if( NULL == fscanf( tfile, "%s", symbol ) ){ /* end of file         */
  325.       fclose( tfile );
  326.       return; }
  327.     else{                    /* insert new symbol    */
  328.       Exceptions[i++] = strsave( upper(symbol) );
  329.       }
  330.     }
  331.  
  332.   printf("ERROR: Language Definition File is too long (Max = %d).\n", MAX_DEF );
  333.   exit();
  334.  
  335. }
  336.